package Servlets;

import Bean.User;
import Util.GokJDBCUtil2;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

@javax.servlet.annotation.WebServlet(name = "LoginUserServlet", urlPatterns = "/LoginUserServlet")
public class LoginUserServlet extends javax.servlet.http.HttpServlet {
    @Override
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
       doGet(request, response);
    }

    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        int id = Integer.parseInt(request.getParameter("username"));
        String password = request.getParameter("password");
        String sql = "select * from user where id=? and userpassword=?";
        User user = GokJDBCUtil2.queryForObject(sql, User.class, id, password);
        if(user != null){
            HttpSession session = request.getSession();
            session.setAttribute("user",user);
            System.out.println(session);
            request.getRequestDispatcher("pages/adminIndex.jsp").forward(request,response);
        }
        else{
            response.sendRedirect("/pages/login.jsp");
        }
        writer.close();
    }
}
